package com.sdp.khy.baotou.SendSms;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponseBody;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.Random;
import java.util.concurrent.CompletableFuture;


public class SendSmsController {
   // private static String ALIBABA_CLOUD_ACCESS_KEY_ID="LTAI5tKeWJhMvw61cYK4SWsw";
   // private static String ALIBABA_CLOUD_ACCESS_KEY_SECRET="oCgZmAuA7zpEAkEnPB19iAt4NyWZGV";
     // 包头
     private static String ALIBABA_CLOUD_ACCESS_KEY_ID="LTAI5tKKkRESgce3hqqUJ26K";
     private static String ALIBABA_CLOUD_ACCESS_KEY_SECRET="7htWYRhkJrOnL2fMP9BbViJxas3p43";

    public static String sendSms(JSONObject jsonObject) throws Exception {
        String phoneNumbers=jsonObject.get("phoneNumbers").toString();
        Random random=new Random();
        int code= random.nextInt(900000)+100000;
        JSONObject templateParam=new JSONObject();
        templateParam.put("code",String.valueOf(code));
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID)
                .accessKeySecret(ALIBABA_CLOUD_ACCESS_KEY_SECRET)
                .build());
        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai")
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("包头市公运速驰物流")
                .templateCode("SMS_294125255")
                .phoneNumbers(phoneNumbers)
                .templateParam(templateParam.toString())
                .build();
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        SendSmsResponseBody body=resp.getBody();
        if(body.getCode().equals("OK")){
            client.close();
            return String.valueOf(code);
        }
        return "";
    }
    
    public static String sendReminderSms(JSONObject jsonObject) throws Exception {
    	String phoneNumbers=jsonObject.get("phoneNumbers").toString();

        JSONObject templateParam=new JSONObject();
        templateParam.put("station", jsonObject.getString("station"));
        templateParam.put("name", jsonObject.getString("name"));
        
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID)
                .accessKeySecret(ALIBABA_CLOUD_ACCESS_KEY_SECRET)
                .build());
        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai")
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();
        
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("包头市公运速驰物流")
                .templateCode("SMS_465376477")
                .phoneNumbers(phoneNumbers)
                .templateParam(templateParam.toString())
                .build();
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        SendSmsResponseBody body=resp.getBody();
        if(body.getCode().equals("OK")){
            client.close();
            return "1";
        }
        return "0";
    }
    

    public static void main(String[] args) {
        try {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("phoneNumbers","15824889229");
            jsonObject.put("name","123456789qwer");
            jsonObject.put("station","萨拉齐站");
            sendReminderSms(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
